<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Public_Controller
{
	/**
	 * The default segment
	 * @access private
	 * @var string
	 */
	private $default_segment = 'home';
	
	/**
	 * Constructor method
	 * @access public
	 * @return void
	 */
    public function __construct() 
    {
    	parent::Public_Controller();
        $this->load->model('galleries/category_m');
        $this->load->model('galleries/gallery_m');
        
        // This basically keeps links to /home always pointing to the actual homepage even when the default_controller is changed
		@include(APPPATH.'config/routes.php'); // simple hack to get the default_controller, could find another way.
		
		$current_page = $this->uri->segment(1, $this->default_segment);
		$this->template->set('current_page', $current_page);
		
		// This will be interesting later
		$this->viewing_homepage = $this->uri->segment(1, $this->default_segment) == $this->default_segment;
		if ($this->viewing_homepage AND $route['default_controller'] != 'site/home')
		{
			redirect('');
		}
    }
    public function index()
    {
		$this->template
    		->set_layout(FALSE)
    		->set('site_name', 'Home')
			->build('index');
    }
    public function welcome()
    {
		$this->template
    		->set('site_name', 'Home')
			->build('home');
    }
    public function company()
    {
		$this->template
    		->set('site_name', 'Company')
			->build('company');
    }
    public function contactus()
    {
    	$rules = array(
			array(
				'field'	=> 'contact_person',
				'label'	=> 'Contact Person',
				'rules'	=> 'required|trim|max_length[30]'
			),
			array(
				'field'	=> 'contact_email',
				'label'	=> 'E-mail',
				'rules'	=> 'required|trim|valid_email|max_length[80]'
			),
			array(
				'field'	=> 'company_name',
				'label'	=> 'Company Name',
				'rules'	=> 'required|trim|max_length[80]'
			),
			array(
				'field'	=> 'address',
				'label'	=> 'Address',
				'rules'	=> 'trim'
			),
			array(
				'field'	=> 'fax',
				'label'	=> 'Fax',
				'rules'	=> 'trim'
			),
			array(
				'field'	=> 'message',
				'label'	=> 'Message',
				'rules'	=> 'trim|max_length[400]'
			)
		);
		
		$this->load->library('form_validation');
		$this->load->library('email');
		$this->load->helper('form');

		$this->form_validation->set_rules($rules);
		
		// If the user has provided valid information
		if ($this->form_validation->run())
		{
			// The try to send the email
			if ($this->_send_email())
			{
				// Store this session to limit useage
				$this->session->set_flashdata('success', 'Thank you for your reply!');
			}

			else
			{
				$this->session->set_flashdata('error', 'Sorry, system is busy now, contact us later.');
			}

			redirect(current_url());
		}
		$postdata = array();
   	    foreach(array_keys($_POST) as $field_name)
		{
			$post_data[$field_name] = $this->input->post($field_name, TRUE);
		}

    	$this->template
//    		->set_layout('product')
    		->set('site_name', 'Contact Us')
    		->set('post_data', $postdata)
			->build('contactus');
    }
    
    function _send_email()
	{
		$this->load->library('email');
		$this->load->library('user_agent');
		$this->email->from($this->settings->item('contact_email'));
		$this->email->to($this->settings->item('contact_email'));

		// If "other subject" exists then use it, if not then use the selected subject
		$subject = 'customer contact us';
		$this->email->subject($this->settings->item('site_name') .' - '.$subject);

		// Loop through cleaning data and inputting to $data
		foreach(array_keys($_POST) as $field_name)
		{
			$data[$field_name] = $this->input->post($field_name, TRUE);
		}

		// Add in some extra details
		$data['sender_agent']	=	$this->agent->browser().' '.$this->agent->version();
		$data['sender_ip']		=	$this->input->ip_address();
		$data['sender_os']		=	$this->agent->platform();

		$this->email->message($this->load->view('email/contact_html', $data, TRUE));
		$this->email->set_alt_message($this->load->view('email/contact_plain', $data, TRUE));

		// If the email has sent with no known erros, show the message
		return (bool) $this->email->send();
	}    
    
    public function category($category_id = 0)
    {
		$category = $this->category_m->get($category_id);
		
		$pagination = create_pagination('category/'. $category_id. '/' , $this->gallery_m->count_galleries_by_category($category_id), 9, 3);
    	$galleries = $this->gallery_m->get_galleries_by_category($category_id, $pagination['limit']);
		
    	$this->template
    		->set_layout('product')
    		->set('site_name', 'Products')
    		->set('galleries', $galleries)
    		->set('pagination', $pagination)
    		->set('category', $category)
			->build('galleries/category_product');
    }
    public function product($product_id = 0)
    {
		$info = $this->gallery_m->get($product_id);
    	$this->template
    		->set_layout(FALSE)
    		->set('site_name', $info->title)
    		->set('gallery', $info)
			->build('product');
    }
    public function products()
    {
		$categories = $this->category_m->get_many_by(array('status' => 'active'));
    	$this->template
    		->set('site_name', 'Products')
    		->set('categories', $categories)
			->build('list_product');
    }
}